package org.tokaf;

import java.util.Comparator;

import org.tokaf.datasearcher.DataSearcher;
import org.tokaf.rater.Rater;

/**
 * <p> MyTopKCompareValuesOptional compares two TopKElements with their ratings
 * computed by given rater and threshold. </p> <p> Copyright (c) 2006 </p> <p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class MyTopKCompareValuesOptional implements Comparator {
	boolean descending = true;

	Rater r;

	DataSearcher[] data;

	TopKElement optional;

	public MyTopKCompareValuesOptional(boolean descending, Rater r,
			DataSearcher[] data, TopKElement optional) {
		this.descending = descending;
		this.r = r;
		this.data = data;
		this.optional = optional;
	}

	public MyTopKCompareValuesOptional(Rater r, DataSearcher[] data,
			TopKElement optional) {
		this(true, r, data, optional);
	}

	public int compare(Object o1, Object o2) {
		if (r.getRating((TopKElement) o1, data, optional) > r.getRating(
				(TopKElement) o2, data, optional)) {
			if (descending)
				return -1;
			else
				return 1;
		} else if (r.getRating((TopKElement) o1, data, optional) < r.getRating(
				(TopKElement) o2, data, optional)) {
			if (descending)
				return 1;
			else
				return -1;
		} else
			return 0;
	}
}
